package club.hicode.daydayup.redis.lettuce;

import club.hicode.daydayup.lambda.Person;
import club.hicode.daydayup.protostuff.ProtostuffUtil;
import club.hicode.daydayup.redis.lettuce.queue.Provider;
import club.hicode.daydayup.redis.lettuce.queue.RedisQueue;
import cn.hutool.core.util.StrUtil;
import com.github.jsonzou.jmockdata.JMockData;
import io.lettuce.core.KeyValue;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.codec.ByteArrayCodec;
import org.junit.Test;

import java.time.temporal.ValueRange;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * lettuce 的学习测试
 *
 * @author Liu Chunfu
 * @date 2018-05-04 下午3:00
 **/
public class LettuceLearn {


	@Test
	public void testRedisQueue() {
		RedisQueue redisQueue = new RedisQueue();
		KeyValue keyValue = redisQueue.get(5L);
		System.out.println(keyValue == null);
	}

	@Test
	public void testProvider() {
		Provider provider = new Provider();
		//provider.task("45", 10L);
		provider.consumer();
	}

	@Test
	public void testTime() {
		System.out.println(System.currentTimeMillis());
	}

	@Test
	public void test1() {
		RedisClient redisClient = RedisClient.create("redis://@localhost:6379/0");
		StatefulRedisConnection<String, String> connection = redisClient.connect();
		RedisCommands<String, String> syncCommands = connection.sync();
		//syncCommands.set("key", "Hello, Redis!");
		String key = syncCommands.get("key");
		System.out.println(key);
		connection.close();
		redisClient.shutdown();
	}

	/**
	 * key和value 都必须是byte
	 */
	@Test
	public void test2() {
		RedisClient redisClient = RedisClient.create("redis://@localhost:6379/0");
		StatefulRedisConnection<byte[], byte[]> connect = redisClient.connect(new ByteArrayCodec());

		byte[] key = StrUtil.bytes("person:1");
		Person person = JMockData.mock(Person.class);
		String set = connect.sync().set(key, ProtostuffUtil.serializer(person));
		System.out.println("result is :" + set);
		byte[] bytes = connect.sync().get(key);
		Person person1 = ProtostuffUtil.deserializer(bytes, Person.class);
		System.out.println(person1.equals(person));
		System.out.println(person1);
	}

	@Test
	public void test3() {
		RedisClient redisClient = RedisClient.create("redis://@localhost:6379/0");
		StatefulRedisConnection<String, String> connection = redisClient.connect();
		RedisCommands<String, String> commands = connection.sync();
		//设置如果不存在
		//commands.setnx()

		Long lpush = commands.lpush("per:stus", "1", "2", "3");

		String lpop = commands.lpop("per:stus");
		System.out.println(lpop);

	}

	@Test
	public void test4() throws ExecutionException, InterruptedException {
		RedisClient client = RedisClient.create(new RedisURI("localhost", 6379, 4, TimeUnit.MINUTES));
		StatefulRedisConnection<String, String> connect = client.connect();
		RedisAsyncCommands<String, String> commands = connect.async();
		RedisFuture<String> set = commands.set("person:age", "28");
		String s = set.get();
		System.out.println(s);
	}

	/**
	 * pf进行UV测试
	 */
	@Test
	public void testHyperLogLog() {
		RedisClient redisClient = RedisClient.create("redis://@localhost:6379/0");
		StatefulRedisConnection<String, String> connect = redisClient.connect();
		RedisCommands<String, String> commands = connect.sync();
		int max = 100000; //99567
		String key = "lcf-hyper";
		for (int i = 0; i < max; i++) {
			commands.pfadd(key, i + "");
		}

		Long pfcount = commands.pfcount(key);
		System.out.println(pfcount);
	}

	/**
	 * 布隆过滤器
	 */
	@Test
	public void testBloomFilter() {
		RedisClient redisClient = RedisClient.create("redis://@localhost:6379/0");
		StatefulRedisConnection<String, String> connect = redisClient.connect();
		RedisCommands<String, String> commands = connect.sync();

	}


}
